package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.DataTaskConf;
import com.belf.console.service.IDataTaskConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 同步任务Controller
 * 
 * @author belf
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/console/dataTaskconf")
public class DataTaskConfController extends BaseController
{
    @Autowired
    private IDataTaskConfService dataTaskConfService;

    /**
     * 查询同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataTaskConf dataTaskConf)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataTaskConf.setCreateBy(getUsername());
        }
        List<DataTaskConf> list = dataTaskConfService.selectDataTaskConfList(dataTaskConf);
        long count = dataTaskConfService.selectDataCount(dataTaskConf);
        return getDataTablePost(list,count);
    }

    /**
     * 导出同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:export')")
    @Log(title = "同步任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataTaskConf dataTaskConf)
    {
        List<DataTaskConf> list = dataTaskConfService.selectDataTaskConfList(dataTaskConf);
        ExcelUtil<DataTaskConf> util = new ExcelUtil<DataTaskConf>(DataTaskConf.class);
        util.exportExcel(response, list, "同步任务数据");
    }

    /**
     * 获取同步任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataTaskConf dataTaskConf)
    {
        return success(dataTaskConfService.selectDataTaskConfById(dataTaskConf.getId()));
    }

    /**
     * 新增同步任务
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:add')")
    @Log(title = "同步任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataTaskConf dataTaskConf)
    {
        dataTaskConf.setUpdateBy(getUsername());
        dataTaskConf.setCreateBy(getUsername());
        return toAjax(dataTaskConfService.insertDataTaskConf(dataTaskConf));
    }

    /**
     * 修改同步任务
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:edit')")
    @Log(title = "同步任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataTaskConf dataTaskConf)
    {
        dataTaskConf.setUpdateBy(getUsername());
        return toAjax(dataTaskConfService.updateDataTaskConf(dataTaskConf));
    }

    /**
     * 删除同步任务
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:remove')")
    @Log(title = "同步任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataTaskConf dataTaskConf)
    {
        return toAjax(dataTaskConfService.deleteDataTaskConfByIds(dataTaskConf.getIdArray()));
    }

    /**
     * 执行评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:edit')")
    @Log(title = "同步任务", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody DataTaskConf dataTaskConf)
    {
        return dataTaskConfService.executeTaskById(dataTaskConf.getId());
    }

    /**
     * 启动调度器
     */
    @PreAuthorize("@ss.hasPermi('console:dataTaskconf:edit')")
    @Log(title = "同步任务", businessType = BusinessType.UPDATE)
    @PostMapping("/scheduler")
    public AjaxResult scheduler(@RequestBody DataTaskConf dataTaskConf)
    {
        return dataTaskConfService.schedulerTask(dataTaskConf.getIdArray());
    }

    /**
     * 执行评估任务
     */
    @Log(title = "校验任务", businessType = BusinessType.UPDATE)
    @PostMapping("/verify")
    public AjaxResult verify(@RequestBody DataTaskConf dataTaskConf)
    {
        return dataTaskConfService.verify(dataTaskConf);
    }
}
